package chaos.tool;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.Callable;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;

import com.jme.input.KeyInput;
import com.jme.system.DisplaySystem;
import com.jme.system.canvas.JMECanvasImplementor;
import com.jme.util.GameTaskQueueManager;

import com.jmex.awt.input.AWTMouseInput;

public class MainFrame extends JFrame {
	private int width = 640;
	private int height = 480;
    JPanel contentPane;
    JPanel mainPanel = new JPanel();
    Canvas comp = null;
    JButton coolButton = new JButton();
    JButton uncoolButton = new JButton();
    JPanel spPanel = new JPanel();
    JScrollPane scrollPane = new JScrollPane();
    JTree jTree1 = new JTree();
    JCheckBox scaleBox = new JCheckBox("Scale GL Image");
    JPanel colorPanel = new JPanel();
    JLabel colorLabel = new JLabel("BG Color:");
    JMECanvasImplementor impl;

    // Construct the frame
    public MainFrame() {
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                dispose();
            }
        });
    }
    
    public void paintGLCanvas() {
    	if(comp == null) return;
    	comp.repaint();
    }

    // Component initialization
    public void init(XDCanvas canvas) {
        contentPane = (JPanel) this.getContentPane();
        contentPane.setLayout(new BorderLayout());

        mainPanel.setLayout(new GridBagLayout());

        setTitle("UNITEditor - v0.2b");

        comp = canvas.getGlCanvas();
        
        coolButton.setText("Cool Button");
        uncoolButton.setText("Uncool Button");

        colorPanel.setBackground(java.awt.Color.black);
        colorPanel.setToolTipText("Click here to change Panel BG color.");
        colorPanel.setBorder(BorderFactory.createRaisedBevelBorder());
        colorPanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                final java.awt.Color color = JColorChooser.showDialog(
                        MainFrame.this, "Choose new background color:",
                        colorPanel.getBackground());
                if (color == null)
                    return;
                colorPanel.setBackground(color);
                Callable<?> call = new Callable<Object>() {
                    public Object call() throws Exception {
                        comp.setBackground(color);
                        return null;
                    }
                };
                GameTaskQueueManager.getManager().render(call);
            }
        });

        scaleBox.setOpaque(false);
        scaleBox.setSelected(true);
        scaleBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (comp != null)
                    doResize();
            }
        });

        spPanel.setLayout(new BorderLayout());
        contentPane.add(mainPanel, BorderLayout.WEST);
        mainPanel.add(scaleBox,
                new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER,
                        GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0,
                                5), 0, 0));
        mainPanel.add(colorLabel,
                new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER,
                        GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0,
                                5), 0, 0));
        mainPanel.add(colorPanel, new GridBagConstraints(0, 2, 1, 1, 0.0,
                0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
                new Insets(5, 5, 0, 5), 25, 25));
        mainPanel.add(coolButton,
                new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER,
                        GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0,
                                5), 0, 0));
        mainPanel.add(uncoolButton,
                new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER,
                        GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0,
                                5), 0, 0));
        mainPanel.add(spPanel, new GridBagConstraints(0, 5, 1, 1, 1.0, 1.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(5, 5, 0, 5), 0, 0));
        spPanel.add(scrollPane, BorderLayout.CENTER);
        
        scrollPane.setViewportView(jTree1);
        comp.setBounds(0, 0, width, height);
        contentPane.add(comp, BorderLayout.CENTER);
    }
    
    protected void doResize() {
        if (scaleBox != null && scaleBox.isSelected()) {
            impl.resizeCanvas(comp.getWidth(), comp.getHeight());
        } else {
            impl.resizeCanvas(width, height);
        }
    }

    // Overridden so we can exit when window is closed
    protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
            System.exit(0);
        }
    }
}